package com.ruoyi.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.core.page.Query;
import com.ruoyi.common.query.QueryTool;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.work.domain.*;
import com.ruoyi.work.service.IWorkBillDetailService;
import com.ruoyi.work.service.IWorkBillKilnService;
import com.ruoyi.work.service.IWorkBillStaffsService;
import com.sun.corba.se.spi.orbutil.threadpool.Work;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.work.mapper.WorkBillMapper;
import com.ruoyi.work.service.IWorkBillService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 计件工作清单Service业务层处理
 * 
 * @author xwj
 * @date 2024-08-14
 */
@Service
public class WorkBillServiceImpl extends ServiceImpl<WorkBillMapper, WorkBill> implements IWorkBillService {
    @Autowired
    private WorkBillMapper workBillMapper;
    @Autowired
    private IWorkBillDetailService workBillDetailService;
    @Autowired
    private IWorkBillStaffsService workBillStaffsService;
    @Autowired
    private IWorkBillKilnService workBillKilnService;

    /**
     * 查询计件工作清单
     * 
     * @param id 计件工作清单主键
     * @return 计件工作清单
     */
    @Override
    public WorkBill selectWorkBillById(Long id) {
        WorkBill workBill = workBillMapper.selectWorkBillById(id);

        WorkBillDetail param = new WorkBillDetail();
        param.setBillId(id);
        List<WorkBillDetail> details = workBillDetailService.selectWorkBillDetailList(param);
        workBill.setDetailList(details);


        WorkBillKiln param2 = new WorkBillKiln();
        param2.setBillId(id);
        List<WorkBillKiln> kilnList = workBillKilnService.selectWorkBillKilnList(param2);
        workBill.setKilnList(kilnList);

        return workBill;
    }

    /**
     * 查询计件工作清单列表
     * 
     * @param workBill 计件工作清单
     * @return 计件工作清单
     */
    @Override
    public List<WorkBill> selectWorkBillList(WorkBill workBill) {
        return workBillMapper.selectWorkBillList(workBill);
    }

    /**
     * 新增计件工作清单
     * 
     * @param workBill 计件工作清单
     * @return 结果
     */
    @Override
    @Transactional
    public WorkBill insertWorkBill(WorkBill workBill) {
        save(workBill);
        insertWorkBillDetail(workBill);
        insertWorkBillStaffs(workBill);
        insertWorkBillKiln(workBill);
        return workBill;
    }

    /**
     * 修改计件工作清单
     * 
     * @param workBill 计件工作清单
     * @return 结果
     */
    @Override
    @Transactional
    public WorkBill updateWorkBill(WorkBill workBill) {
        insertWorkBillDetail(workBill);
        insertWorkBillStaffs(workBill);
        insertWorkBillKiln(workBill);
        updateById(workBill);
        return workBill;
    }

    /**
     * 工单详情
     * @param workBill
     */
    private void insertWorkBillDetail(WorkBill workBill) {
        List<WorkBillDetail> details = workBill.getDetailList();
        Long id = workBill.getId();;
        Date now = new Date();
        if(StringUtils.isNotNull(details)) {
            for(WorkBillDetail detail : details) {
                detail.setBillId(id);

                if(detail.getId() == null) {
                    detail.setCreateTime(now);
                    workBillDetailService.insertWorkBillDetail(detail);
                } else {
                    detail.setUpdateTime(now);
                    workBillDetailService.updateWorkBillDetail(detail);
                }
            }
        }

        // 要删除的数据
        List<Long> deleteIds = workBill.getDeleteIds();
        if(deleteIds != null && deleteIds.size() > 0) {
            Long[] ids = deleteIds.toArray(new Long[0]);
            workBillDetailService.deleteWorkBillDetailByIds(ids);
        }
    }

    /**
     * 窑烧详情
     * @param workBill
     */
    private void insertWorkBillKiln(WorkBill workBill) {
        List<WorkBillKiln> kilnList = workBill.getKilnList();
        Long id = workBill.getId();;
        Date now = new Date();

        if(StringUtils.isNotNull(kilnList)) {
            for(WorkBillKiln kiln: kilnList) {
                kiln.setBillId(id);
                if(kiln.getId() == null) {
                    kiln.setCreateTime(now);
                    workBillKilnService.insertWorkBillKiln(kiln);
                } else {
                    kiln.setUpdateTime(now);
                    workBillKilnService.updateWorkBillKiln(kiln);
                }
            }
        }

        // 要删除的数据
        List<Long> deleteIds = workBill.getDeleteKilnIds();
        if(deleteIds != null && deleteIds.size() > 0) {
            Long[] ids = deleteIds.toArray(new Long[0]);
            workBillKilnService.deleteWorkBillKilnByIds(ids);
        }
    }

    /**
     * 工单共享员工
     * @param workBill
     */
    private void insertWorkBillStaffs(WorkBill workBill) {
        // 删除
        int num = workBillMapper.deleteWorkBillStaffs(workBill.getId());
        List<WorkBillStaffs> staffs = workBill.getStaffs();
        if(staffs != null && staffs.size() > 0) {
            for(WorkBillStaffs workBillStaffs : staffs) {
                workBillStaffs.setBillId(workBill.getId());
                workBillStaffsService.insertWorkBillStaffs(workBillStaffs);
            }
        }
    }

    /**
     * 批量删除计件工作清单
     * 
     * @param ids 需要删除的计件工作清单主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteWorkBillByIds(Long[] ids) {
        workBillDetailService.deleteWorkBillDetailByBillIds(ids);
        int num = workBillMapper.deleteWorkBillByIds(ids);
        return num;
    }

    /**
    * 条件删除
    * @param workBill 计件工作清单
    * @return
    */
    @Override
    public int delete(WorkBill workBill) {
        QueryWrapper<WorkBill> qw = new QueryWrapper<>();
        QueryTool.condition(qw, workBill);
        return baseMapper.delete(qw);
    }

    /**
     * mp分页
     * @param workBill 计件工作清单
     * @return
     */
    @Override
    public Pages<WorkBill> queryPage(WorkBill workBill) {
        QueryWrapper<WorkBill> qw = new QueryWrapper<>();
        QueryTool.condition(qw, workBill);
        IPage<WorkBill> ipage = new Query<WorkBill>().getPage(workBill);

        IPage<WorkBill> page = this.page(
                ipage,
                qw
        );
        return new Pages<>(page);
    }

    @Override
    public boolean checkNo(WorkBill workBill) {
        String no = workBill.getNo();

        QueryWrapper<WorkBill> qw = new QueryWrapper<>();
        qw.eq("no", no);
        qw.eq(workBill.getId() != null, "id", workBill.getId());

        List<WorkBill> list = workBillMapper.selectList(qw);
        int size = 0;
        if(list != null) {
            size = list.size();
        }
        // 新增
        if(workBill.getId() == null && size == 0) {
            return true;
        }

        if(workBill.getId() != null && size == 1) {
            return true;
        }

        return false;
    }

    /**
     * 工单报表
     * @param billReportVo
     * @return
     */
    @Override
    public List<BillReportVo> queryBillReport(BillReportVo billReportVo) {
        return workBillMapper.queryBillReport(billReportVo);
    }

    /**
     * 删除计件工作清单信息
     * 
     * @param id 计件工作清单主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteWorkBillById(Long id) {
        Long []billId = {id};
        workBillDetailService.deleteWorkBillDetailByBillIds(billId);
        workBillStaffsService.deleteWorkBillStaffsByBillId(id);
        return workBillMapper.deleteWorkBillById(id);
    }
}
